// 登录状态管理工具

// 检查是否已登录
export const isLoggedIn = (): boolean => {
  return localStorage.getItem('isLoggedIn') === 'true';
};

// 获取用户手机号
export const getUserPhone = (): string | null => {
  return localStorage.getItem('userPhone');
};

// 获取用户信息
export const getUserInfo = (): any => {
  const userInfo = localStorage.getItem('userInfo');
  return userInfo ? JSON.parse(userInfo) : null;
};

// 保存登录状态
export const setLoginStatus = (phone: string, userInfo?: any): void => {
  localStorage.setItem('isLoggedIn', 'true');
  localStorage.setItem('userPhone', phone);
  if (userInfo) {
    localStorage.setItem('userInfo', JSON.stringify(userInfo));
  }
};

// 退出登录
export const logout = (): void => {
  localStorage.removeItem('isLoggedIn');
  localStorage.removeItem('userPhone');
  localStorage.removeItem('userInfo');
};

// 检查登录状态并跳转
export const checkAuth = (navigate: any): boolean => {
  if (!isLoggedIn()) {
    navigate('/login');
    return false;
  }
  return true;
};

